<script setup>
import { ref } from 'vue'
import { Plus } from "@element-plus/icons-vue";
import useTable from '@/utils/useTable'
import {http} from '@/api'
import {ElMessage} from "element-plus";

/**
 * 搜索
 */
const dateRange = ref([])
const changeDate = (val) => {
  if (!val) {
    searchParams.value.startCreateTime = ''
    searchParams.value.endCreateTime = ''
    return
  }
  searchParams.value.startCreateTime = val[0] || ''
  searchParams.value.endCreateTime = val[1] || ''
}
/**
 * 表格
 * @type {Ref<UnwrapRef<*[]>>}
 */
const {
  searchParams,
  pageNo,
  pageSize,
  total,
  changePage,
  changeSize,
  dataSource,
  search,
  reset,
  openAdd,
  openView,
  openEdit,
  openDelete
} = useTable({
  requestFn: http.user,
  routerPre: 'user',
  params: {
    nick: '',
    mobile: '',
    startCreateTime: '',
    endCreateTime: '',
  }
})

const changeStatus = (row) => {
  http.user.updateStatus({
    id: row.id,
    status: row.status
  }).then(res => {
    ElMessage.success('操作成功')
  }).catch(() => {
    if (row.status === 1) {
      row.status = 0
    } else {
      row.status = 1
    }
  })
}
</script>

<template>
  <PageMain title="用户管理">
    <el-form label-suffix="：" inline>
      <el-form-item label="用户昵称">
        <el-input clearable v-model="searchParams.nick"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input clearable v-model="searchParams.mobile"></el-input>
      </el-form-item>
      <el-form-item label="注册时间">
        <el-date-picker clearable
            v-model="dateRange"
            @change="changeDate"
            type="daterange"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="add-box">
      <el-button type="primary" :icon="Plus" @click="openAdd">添加</el-button>
    </div>
    <div class="table-box">
      <el-table :data="dataSource">
        <el-table-column label="用户昵称" prop="nick"></el-table-column>
        <el-table-column label="用户头像">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <ViewImage type="avatar" :image-url="scope.row.imageUrl"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="mobile"></el-table-column>
        <el-table-column label="注册时间" prop="createTime"></el-table-column>
        <el-table-column label="启用状态">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <el-switch v-model="scope.row.status" :active-value="0" :inactive-value="1" @change="() => changeStatus(scope.row)"></el-switch>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template #default="scope">
            <el-button type="text" @click="openView(scope.row)">查看</el-button>
            <el-button type="text" @click="openEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="openDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[10, 50, 100, 200]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="changeSize"
          @current-change="changePage"
      />
    </div>
  </PageMain>
</template>

<style scoped lang="scss">

</style>
